<template>
	<zh-form :span="24" ref="defPageForm" :model="state.form" :rules="state.rules">
		<zh-form-select
            prop="userType"
            label="用户类型"
            placeholder="单选"
            :options="state.dictData.userType"
            v-model="state.form.userType"
			:allowClear="false"
        ></zh-form-select>
		<zh-form-select
            prop="objetcType"
            label="授权类型"
            placeholder="单选"
			@change="objectTypeChange"
            :options="state.dictData.objectType"
            v-model="state.form.objetcType"
			:allowClear="false"
        ></zh-form-select>
        <zh-form-select
			prop="objectId"
			:lazy="true"
			label="权限名称"
			v-model="state.form.objectId"
			:searchParams="state.searchParams"
			:fieldNames="{ label: 'objetcName', value: 'objetcId' }"
			funcCode="sysDefaultPrivilege0007"
			searchField="objectName"
			:allowClear="false"
		></zh-form-select>    
	</zh-form>
</template>

<script lang="ts" setup name="defaultPriForm">
//------------------接收传入参数(组件)-------------------
const props = defineProps({
	info: {
		type: Object,
		default: () => {},
		required: false,
	},
	modelValue: Boolean,
});
const emit = defineEmits(['update:modelValue']);

//------------------定义子组件---------------------------
const defPageForm = ref<any>();
//------------------内部变量定义-------------------------
const state = reactive({
	dictData: [] as any,
	form: {objetcType:'role' } as any,
	searchParams: {objetcType:'role'} as any,
	rules: {
		objetcType: [{ required: true, message: '请选择授权类型' }],
		userType: [{ required: true, message: '请选择用户类型' }],
		objectId: [{ required: true, message: '请选择授权的权限' }],
	},
});

//------------------定义调用方法-------------------------
const objectTypeChange=(val)=>{
	state.searchParams.objectId=''
	state.searchParams.objetcType=val
}
const save= async () =>{
  //校验表单
	let checkFlag = await defPageForm.value.checkForm();
	if (checkFlag) {
    let saveForm=state.form;
		//校验通过
    if (!saveForm.id) {
		let res = await $api.common.send.post('sysDefaultPrivilege0004',saveForm);
		res.saveType = 'add';
      	emit('update:modelValue', false);
		return res;
	} else {
		let res = await $api.common.send.post('sysDefaultPrivilege0005',saveForm);
		res.saveType = 'edit';
      	emit('update:modelValue', false);
		return res;
	}
  }
}

//------------------初始化------------------------------
// 数据初始化
const initDict=()=>{
  const params = {
    dictFields: 'userType,objectType',
  };
  $api.common.getBatchDict.post(params).then((res) => {
    state.dictData = res.result;
  });
}
state.form = Object.assign(state.form, props.info);
initDict()
//------------设置给父组件调用的数据/方法(组件)-----------
defineExpose({ form: state.form ,save});
</script>
